import datetime
from itertools import compress
import random

from flask_apscheduler import json

from RootPath import RootPath
import os
from common.YmlUtil import YmlUtil
path = os.path.join(RootPath.getRootPath(), "resources/CumulativeData.yml")
class FrequencyValues():

    numb = ["No1", "No2", "No3"]
    #每半个小时更新RunningTime和FrequencyRunningTime累计时间和FrequencyTotalKWH变频器功耗
    def RunningTime(self):
        RunningTime = YmlUtil().readYml(path,"Rtime","RunningTime")
        TotalKWH = YmlUtil().readYml(path,"TotalKWH","FrequencyTotalKWH")
        time = YmlUtil().readYml(path,"time","time")
        RunningTime = int(RunningTime) + 10
        TotalKWH = float(TotalKWH) + random.randint(5,10)/10
        if str(time).find("00"):
            runFlag=random.randint(1, 3)
            YmlUtil().writeYml(path,"RunPump","pumpNO",str(runFlag))
            YmlUtil().writeYml(path,"Rtime","RunningTime",'0')
            YmlUtil().writeYml(path,"TotalKWH","FrequencyTotalKWH",'0')
        else:
            YmlUtil().writeYml(path,"Rtime","RunningTime",str(RunningTime))
            YmlUtil().writeYml(path,"TotalKWH","FrequencyTotalKWH",str(TotalKWH))

    def updatePump(self):
        time = ( datetime.datetime.now()).strftime("%M")
        YmlUtil().writeYml(path,"time","time",time)

    def value(self):
        dtime = ( datetime.datetime.now()).strftime("%H")
        if dtime in range(7, 10):
            runFlag = 1
        elif dtime in range(11, 14):
            runFlag = 2
        elif dtime in range(17, 21):
            runFlag = 3
        else:
            runFlag = 0
        #随机那个泵运行
        runFlag=YmlUtil().readYml(path,"RunPump","pumpNO")
        #读取累计运行时间和累计功耗
        RunningTime = YmlUtil().readYml(path,"Rtime","RunningTime")
        TotalKWH = YmlUtil().readYml(path,"TotalKWH","FrequencyTotalKWH")

        frequencys = {
            "FrequencyCurrent": str(random.randint(3, 5)),
            "FrequencyOutput": str(random.randint(20, 50)),
            "FrequencyTemp": str(random.randint(20, 30)),
            "FrequencyPower": str(random.randint(35, 40)),
            "FrequencySpeed": str(random.randint(1000, 3000)),
            # "PowerFrequencyOper":str(random.randint(0,1)),
            "RunningTime":str(RunningTime),
            "FrequencyRunningTime":str(RunningTime),
            "FrequencyTotalKWH":str(TotalKWH),
            "FrequencyOper":"1"
        }
        pointName = {}
        for keys, values in frequencys.items():
            pointName.update({self.numb[int(runFlag) - 1] + keys: values})
            mask = [1] * len(self.numb)
            mask[int(runFlag) - 1] = 0  # 标记runFlag对应的泵号
            for ite in list(compress(self.numb, mask)):
                pointName.update({ite + keys: "0"})
        return pointName

# point_value={}
# value = FrequencyValues().value()
# str ="00"
